package com.yangsen.common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.yangsen.common.model.User;
import com.yangsen.login.LoginService;


public class UserInterceptor implements Interceptor {
    public void intercept(Invocation inv) {
        Controller c = inv.getController();
        User user = LoginService.me.validateToken(c.getCookie(LoginService.USER));
        if (null != user) {

            c.setAttr("user", user);

            if (null == user.getNickname()) {
                c.setAttr("username", user.getUsername());
            } else {
                c.setAttr("username", user.getNickname());
            }
        }
        inv.invoke();
    }
}
